﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Common
{
    public class CMDCalc
    {

        public delegate void StanderdOutputReadstr(string outputstr);

        public static StanderdOutputReadstr outputReadstr;


        public static System.Diagnostics.Process p;
        /// <summary>
        /// 计算模型CMD命令
        /// </summary>
        /// <param name="itemData"></param>
        public static void ExeCalc(Common.TaskItemData itemData,string filename,string arguments)
        {
            //if(string.IsNullOrEmpty(filename))
            //{
            //    MessageBox.Show("filename不能为空");
            //    return;
            //}
            //Common.Log.Info("开始执行计算"+itemData);
            //System.Diagnostics.Process p = new System.Diagnostics.Process();
            //string location = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            //p.StartInfo.FileName = filename;//location + "app.exe";
            //p.StartInfo.Arguments = arguments;//itemData.importpath;
            //p.Start();
            //p.WaitForExit(); 
            Common.Log.Info("计算完成"+itemData);
            System.Threading.Thread.Sleep(1000);
        }

        /// <summary>
        /// 执行多条命令
        /// </summary>
        /// <param name="commands"></param>
        /// <returns></returns>
        public static string ExcuteCommands(string commands)
        {
            p = new System.Diagnostics.Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;

            //启动程序
            p.Start();

            string[] sArray = commands.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string command in sArray)
            {
                //向cmd窗口发送输入信息
                p.StandardInput.WriteLine(command);
            }

            p.StandardInput.AutoFlush = true;

            //获取输出信息
            //string strOuput = p.StandardOutput.ReadToEnd();

            System.Threading.Thread outputThread = new System.Threading.Thread(output);
            outputThread.Start();
            //等待程序执行完退出进程
            //p.WaitForExit();
            System.Threading.Thread.Sleep(100);
            //Console.WriteLine(p.ProcessName);
            

            SetAllProcess("notepad");

            return "";
        }

        public static void output()
        {
            string strOuput = p.StandardOutput.ReadToEnd();
            outputReadstr?.Invoke(strOuput);
            Console.WriteLine(strOuput);
        }


        public static  void SetAllProcess(string processName)
        {
            Process[] GetP = Process.GetProcesses();
            foreach (Process p in GetP)
            {
                if (p.ProcessName == processName)
                {
                    try
                    {
                        p.EnableRaisingEvents = true;
                        p.Exited += new EventHandler(p_Exited);
                    }
                    catch { }
                }
            }
        }

        private static void p_Exited(object sender, EventArgs e)
        {
            //线程退出触发
            Console.WriteLine("进程退出");
            ExitCMD();
        }

        public static void ExitCMD()
        {
            if (p != null && !p.HasExited)
            {
                p.Kill();
            }
        }
    }
}
